package com.mobileservice.autoreply.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mobileservice.autoreply.R;
import com.mobileservice.autoreply.activity.Add_Reply_ContactActivity;
import com.mobileservice.autoreply.activity.AutoReplyActivity;
import com.mobileservice.autoreply.ui.customizeui.CustomSmSTemplateDialog;
import com.mobileservice.autoreply.ui.model.PhoneContactModel;

public class ContactItemAutoReplyAdapter extends ArrayAdapter<PhoneContactModel> {

	private List<PhoneContactModel> contactModels = new ArrayList<PhoneContactModel>();
	public List<PhoneContactModel> SelectedModel = new ArrayList<PhoneContactModel>();
	private LayoutInflater inflater;
	private int layout;

	/** Contructor */
	public ContactItemAutoReplyAdapter(Context context, int layout, List<PhoneContactModel> contactModels) {
		super(context, layout, contactModels);
		// TODO Auto-generated constructor stub
		this.layout = layout;

		if (contactModels != null) {
			this.contactModels = contactModels;
		}

		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return contactModels.size();
	}

	@Override
	public PhoneContactModel getItem(int arg0) {
		return contactModels.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int arg0, View arg1, ViewGroup arg2) {

		final PhoneContactModel model = contactModels.get(arg0);
		Holder holder = null;
		if (arg1 == null) {
			arg1 = inflater.inflate(layout, null);

			holder = new Holder();

			/** Find View by id for Items */
			holder.image = (ImageView) arg1.findViewById(R.id.imageContact);
			holder.ContactName = (TextView) arg1.findViewById(R.id.contactName);
			holder.ContactNumber = (TextView) arg1.findViewById(R.id.contactNumber);
			holder.AutoReplySms = (TextView) arg1.findViewById(R.id.sms_reply_message);
			holder.SelectNum = (CheckBox) arg1.findViewById(R.id.selectionContact);
			holder.auto_reply = (ImageView) arg1.findViewById(R.id.auto_reply);
			holder.mainLayout = (RelativeLayout) arg1.findViewById(R.id.mainLayout);
			/** End */
			arg1.setTag(holder);
		} else {
			holder = (Holder) arg1.getTag();
		}

		holder.ContactName.setText(model.getContactName());
		holder.ContactNumber.setText(model.getContactNumber());
		holder.AutoReplySms.setText(model.getAutoReplyMessage());
		holder.auto_reply.setVisibility(model.isAutoReply() ? View.VISIBLE : View.INVISIBLE);

		/** Need to work again */

		SelectColorSelected(holder, !model.isAutoReply());
		holder.SelectNum.setChecked(model.isAutoReply());

		/** End */

		final Holder template = (Holder) arg1.getTag();
		holder.mainLayout.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View arg0) {
				// TODO Auto-generated method stub
				CustomSmSTemplateDialog dialog = new CustomSmSTemplateDialog(getContext(), model);
				dialog.show();
				return false;
			}
		});
		/** Set Listener */
		holder.SelectNum.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				CheckBox cb = (CheckBox) v;

				contactModels.get(arg0).setAutoReply(cb.isChecked());
				if (getContext() instanceof Add_Reply_ContactActivity) {
					if (cb.isChecked()) {
						SelectedModel.add(contactModels.get(arg0));
					} else {
						SelectedModel.remove(contactModels.get(arg0));

					}
				} else {
					if (cb.isChecked()) {
						SelectedModel.remove(contactModels.get(arg0));
					} else {
						SelectedModel.add(contactModels.get(arg0));
					}
					((AutoReplyActivity) getContext()).topBar.shiftButton(SelectedModel.size() > 0);

				}

				/** Set Topbar Button Become Save Button */

				SelectColorSelected(template, !cb.isChecked());
			}
		});

		return arg1;
	}

	private void SelectColorSelected(Holder holder, boolean isChecked) {
		/** Check color for Contact Name, Number */

		holder.ContactName.setTextColor(getContext().getResources().getColor(isChecked ? R.color.caption_color : R.color.caption_color_selected));
		holder.ContactNumber.setTextColor(getContext().getResources().getColor(isChecked ? R.color.caption_color : R.color.caption_color_selected));
		holder.AutoReplySms.setTextColor(getContext().getResources().getColor(isChecked ? R.color.caption_color : R.color.caption_color_selected));
	}

	private class Holder {
		ImageView image, auto_reply;
		TextView ContactName, ContactNumber, AutoReplySms;
		CheckBox SelectNum;
		RelativeLayout mainLayout;
	}

}
